<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/29/029
 * Time: 14:11
 */

namespace app\Lib\Model\Order;


use app\Lib\Exception\BlException;
use app\Lib\Model\DbModel\OrderBindingUser;
use app\Lib\Model\DbModel\OrderImg;
use app\Lib\Model\DbModel\OrderInfo;
use app\Lib\Model\DbModel\OrderLog;
use app\Lib\Model\DbModel\OrderOtherThanTime;
use app\Lib\Model\DbModel\OrderPanoramicUrl;
use app\Lib\Model\Img\Img;
use app\Lib\Model\User\User;

class GetMySiteInfo
{
    private $user;
    /** @var Order $order */
    private $order;
    public function __construct(User $user)
    {
        $this->user = $user;
    }
    public function setOrder(Order $order){
        $this->order = $order;
    }

    /**
     * @return array
     * @throws BlException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function run(){
        $get_order_number = true;
        if(empty($this->order)){
            $order_number =  $this->getOrderNumber();
            if(empty($order_number)){
                $get_order_number = false;
            }else{
                $this->order =  new Order($order_number);
            }
        }
        $role = $this->user->getRoleClass();
        $isValidate = false;
        if($role && $role->isAdminLogin()){
            $isValidate = true;
        }
        if($isValidate == true && $get_order_number == false){
            $order_number = (new OrderInfo())->value('order_number');
            $this->order =  new Order($order_number);
        }
        if($isValidate == false && $get_order_number == false){
            return [];
        }
        if($this->user->getAccount() == $this->order->get_user_phone()){
            $isValidate = true;
        }
        $db = new OrderBindingUser();
        $id =  $db
            ->where([$db->_user_id=>$this->user->getUid(),$db->_order_id=>$this->order->get_order_id()])
            ->value($db->_id);
        if($id){
            $isValidate = true;
        }
        if($isValidate == false){
            throw new BlException('该工单展示权限未对您开放');
        }
        $startWord = $this->order->get_starting_time();
        $startWord_str = '';
        if($startWord>0){
            $startWord_str= date('Y-m-d',$startWord);
        }
        $supervisor = '';
        if($this->order->get_site_supervisor_uid()>0){
            $supervisor = ($this->order->get_site_supervisor_user())->getNickName();
        }
        $model =  new OrderPanoramicUrl();
        $vrUrl = $model->where([$model->_order_id=>$this->order->get_order_id()])->value($model->_path_url);
        $count = (new OrderLog())->where('order_id',$this->order->get_order_id())->count();
        $info = [
            'orderLogPage'=>ceil($count/10),
            'design_user'=>$this->order->get_design_user()->getNickName(),
            'order_number'=>$this->order->get_order_number(),
            'site_supervisor_uid'=>$supervisor,
            'duration'=>$this->order->get_duration(),
            'startWord'=>$startWord_str,
            'orderStatus'=>$this->order->get_orderStatus_str(),
            'endWord'=>$this->getStopDay(),
            'orderStyle'=>$this->order->get_style_obj()->getTagName(),
            'user_address'=>$this->order->get_user_address().'工地日志',
            'user_address_array'=>$this->getMySite(),
            'VrUrl'=>$vrUrl,
            'orderImg'=>$this->getOrderImg(),
            'orderLog'=>$this->getOrderLog(),
        ];
        return $info;
    }

    private function getOrderImg(){
        $imgDb    = new OrderImg();
        $imgArray = $imgDb->where([$imgDb->_order_id=>$this->order->get_order_id()])->column($imgDb->_img_id);
        $imgData = [];
        if($imgArray){
            foreach ($imgArray as $v){
                $imgModel = new Img($v);
                $imgData[] = $imgModel->getFilePath();
            }
        }
        return $imgData;
    }

    private function getOrderNumber(){
        $db =new OrderInfo();
        $map[$db->_user_phone] = $this->user->getAccount();
        $db =new OrderInfo();
        $order_number =  $db->where($map)->value($db->_order_number);
        if(!$order_number){
            $db = new OrderBindingUser();
            $orderId =  $db->where([$db->_user_id=>$this->user->getUid()])->value($db->_id);
            if($orderId){
                $order_number = Order::getOrderNumber($orderId);
            }
        }
        return $order_number;
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function getOrderLog(){
        $model = new GetOrderLog($this->order);
        return  $model->run();
    }

    private function getStopDay(){
        if($this->order->get_starting_time()<=0){
            return '等待开工';
        }
        $model =  new OrderOtherThanTime();
        $count = $model->where($model->_order_id,$this->order->get_order_id())->count();
        $endTime = $this->order->get_starting_time()+(($this->order->get_duration()+$count)*24*3600);
        return date('Y-m-d',$endTime);
    }

    private function getMySite(){
        $role = $this->user->getRoleClass();
        $order = [];
        $model = (new OrderInfo());
        if($role && $role->isAdminLogin()){
            $orderId = $model->column($model->_id);
        }else{
            $db =new OrderInfo();
            $map[$db->_user_phone] = $this->user->getAccount();
            $map[$db->_id] = ['neq',$this->order->get_order_id()];
            $db =new OrderInfo();
            $orderId =  $db->where($map)->column($db->_id);
            $db = new OrderBindingUser();
            $orderBindingId =  $db
                ->where([$db->_user_id=>$this->user->getUid(),$db->_id=>['neq',$this->order->get_order_id()]])
                ->value($db->_order_id);
            $orderId = empty($orderId)?[]:$orderId;
            $orderBindingId = empty($orderBindingId)?[]:$orderBindingId;
            $orderId = array_merge($orderId,$orderBindingId);
        }
        if($orderId){
            $order = $model->where([$model->_id=>['in',$orderId]])
                ->column($model->_user_address,$model->_order_number);
        }
//        dump($orderId);
        return $order;
    }
}